$(function () {
    layui.use(['table','form','laydate'], function(){
        let table = layui.table;
        let form = layui.form;
        let laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#startTime', //指定元素
            trigger:'click'
        });
        laydate.render({
            elem: '#endTime', //指定元素
            trigger:'click'
        });
        form.on('submit(search)',function (data) {
            data = data.field;
            console.log(data);
           getTable(data);
            return false;
        });
        //获取表格
        getTable({});
        function getTable(data){
            table.render({
                elem: '#newsTable' //前端表格地址id
                ,size: 'lg' //小尺寸的表格
                ,where: data
                ,url:'/back/FindAllNews' //访问java后台路径
                ,cols: [[
                    // {type: 'checkbox', fixed: 'left'}
                    {field:'id', title:'ID', align: 'center', with:50,fixed: 'left',sort: true,}
                    ,{field:'title', title:'标题',align: 'center', width: 150}
                    ,{field:'img', title:'图片',align: 'center',width: 150,templet:function (d) {
                            return "<img src='"+d.img+"' width='100' height='80'>";
                        }}
                    ,{field:'time', title:'发布时间',align: 'center', width: 150}
                    ,{field:'content', title:'内容',align: 'center',width: 220}
                    ,{field:'enable', title:'状态',align: 'center',width: 80,templet: function (d) {
                            return d.enable ==1 ? "<span class='layui-badge layui-bg-green'>启用</span>" : "<span class='layui-badge layui-bg-orange'>禁用</span>";
                        }}
                    ,{field:'remark', title:'备注',align: 'center',width: 120}
                    , {field: 'newsName', title: '新闻类型', sort: true}
                    ,{fixed: 'right', title:'操作', align: 'center',toolbar: '#barDemo', width:150}
                ]]
                ,page: true
            });

        }



        //监听行工具事件
        table.on('tool(newsTable)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    // obj.del();
                    // layer.close(index);
                    $.ajax({
                        url:'/back/DeleteNews',
                        data:{id:data.id},
                        type: 'post',
                        dataType: 'json',
                        success: function (res) {
                            if (res.count>0){
                                layer.msg("删除成功")
                                obj.del();
                                layer.close(index);
                            }else{
                                layer.msg("删除失败");
                            }
                        }
                    })

                });
            } else if(obj.event === 'edit'){
                sessionStorage.setItem('newId',data.id)
                xadmin.open('修改新闻','/html/news/news-update.html',600,400)
            }



        });
    });
})